package syyrjx.controller;

import syyrjx.jdbc.DataBase;
import syyrjx.jdbc.util.JDBCTool;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

public class ShowPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.showInfo(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.showInfo(request, response);
    }
    private void showInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<Integer, Map<String,Object>> uInfo = DataBase.show();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print("<table border=\"1px\">\n");
        out.print("<tr>\n");
        out.print("<td>编号</td>\n");
        out.print("<td>用户名</td>\n");
        out.print("<td>密码</td>\n");
        out.print("<td>性别</td>\n");
        out.print("<td>删除</td>\n");
        out.print("</tr>\n");
        for(int i = 0 ;i < JDBCTool.count;i++){
            Map<String,Object> temp = uInfo.get(i);
            out.print("<tr>");
            out.print("<td>"+temp.get("id")+"</td>");
            out.print("<td>"+temp.get("username")+"</td>");
            out.print("<td>"+temp.get("userpwd")+"</td>");
            out.print("<td>"+temp.get("sex")+"</td>");
            out.print("<td><input type=\"button\" id=\"btn"+i+"\" value=\"删除\"/></td>");
            out.print("</tr>\n");
        }
        out.print("</table>\n");
        out.print("<script type=\"text/javascript\">\n");
        out.print("window.onload = function(){\n");
        for(int i = 0 ;i < JDBCTool.count;i++){
            out.print("var btn"+i+" = document.getElementById(\"btn"+i+"\");\n");
            out.print("btn"+i+".onclick = function(){\n");
            out.print("if(window.confirm(\"是否确认删除\")){");
            out.print("location.href=\"http://localhost:8080/onlineSystem/delete?id="+i+"\"\n");;
            out.print("}\n");;
            out.print("}\n");
        }
        out.print("}\n");
        out.print("</script>");
    }
}
